<template>
  <el-dialog title="确认邮件信息" :visible.sync="currentValue" width="480px">
    <div class="send-message">
      <el-input placeholder="请输入内容" clearable v-model.trim="time">
        <template slot="prepend">
          面试时间
        </template>
      </el-input>

      <el-input placeholder="请输入内容" clearable v-model.trim="job">
        <template slot="prepend">
          面试岗位
        </template>
      </el-input>

      <el-input placeholder="请输入内容" clearable v-model.trim="email">
        <template slot="prepend">
          收件人邮箱
        </template>
      </el-input>

      <el-input placeholder="请输入内容" clearable v-model.trim="name">
        <template slot="prepend">
          收件人姓名
        </template>
      </el-input>

      <el-input placeholder="请输入内容" clearable v-model.trim="contactNumber">
        <template slot="prepend">
          联系人电话
        </template>
      </el-input>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="currentValue = false">取 消</el-button>
      <el-button type="primary" @click="makeSure">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { sendEmail } from '@/api/home'
import ZvModel from '@/mixins/zv-model'
export default {
  name: 'SendEmail',
  mixins: [ZvModel],
  props: {
    infos: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      oldValue: '',
      name: '',
      time: '',
      job: '',
      email: '',
      contactNumber: '15200808387'
    }
  },
  created() {
    this.name = this.infos.name || ''
    this.time = this.infos.time || ''
    this.job = this.infos.job || ''
    this.email = this.infos.email || ''
    this.oldValue = [this.name, this.time, this.job, this.email].toString()
  },
  methods: {
    makeSure() {
      this.currentValue = false
      const newValue = [this.name, this.time, this.job, this.email].toString()
      const params = {
        name: this.name,
        time: this.time,
        job: this.job,
        email: this.email
      }
      if (this.oldValue !== newValue) {
        // 更新数据
        this.$emit('update', {
          ...params,
          id: this.infos._id
        })
      }
      this.send({ ...params, contactNumber: this.contactNumber })
    },
    send(params) {
      if (Object.values(params).indexOf('') > -1) {
        this.$message.error('请确认信息填写正确')
        return
      }

      sendEmail(params).then(data => {
        if (data.success) {
          this.$message({
            type: 'success',
            message: '发送成功'
          })
        } else {
          this.$message({
            type: 'error',
            message: '发送失败'
          })
        }
      })
    }
  }
}
</script>

<style lang="scss">
.send-message {
  .el-input {
    margin-bottom: 20px;
  }
}
</style>
